const urlInput = document.getElementById('videoUrl');
const downloadBtn = document.getElementById('downloadBtn');
const platformSelect = document.getElementById('platform');
const platformInstructions = document.getElementById('platform-instructions');
// 新增进度条元素
const progressText = document.getElementById('progress-text');
const progressBar = document.getElementById('progress-bar');

platformSelect.addEventListener('change', () => {
    const selectedValue = platformSelect.value;
    if (selectedValue === 'douyin') {
        platformInstructions.innerHTML = `<strong>注意:</strong> 已选择“抖音 (无水印)”专用下载通道。`;
        platformInstructions.style.display = 'block';
    } else {
        platformInstructions.style.display = 'none';
    }
});
platformSelect.dispatchEvent(new Event('change'));

downloadBtn.addEventListener('click', async () => {
    const url = urlInput.value.trim();
    const platform = platformSelect.value;
    if (!url) {
        progressText.textContent = "❌ 请输入有效的视频链接";
        return;
    }
    progressText.textContent = "准备下载中...\n";
    progressBar.value = 0; // 重置进度条
    const result = await window.videoDownloader.download(url, platform);
    if (result.success) {
        progressText.textContent += "\n✅ 下载任务已完成。";
    } else {
        progressText.textContent += `\n❌ 下载失败: ${result.error}`;
    }
});

// 关键改动：监听进度并更新进度条和文本
window.videoDownloader.onProgress((data) => {
    const { status, message, percent } = data;

    // 更新文本信息
    progressText.textContent = `[${status}] ${message}`;

    // 更新进度条
    if (percent !== undefined) {
        progressBar.value = percent;
    }

    if (status === 'completed') {
        progressBar.value = 100;
    } else if (status === 'error') {
        progressBar.value = 0;
    }
});